//
//  PowerValueAlertController.swift
//  SCA
//
//  Created by tjun on 2025/9/23.
//

import UIKit


class PowerValueAlertController: UIViewController {

    private var maskView: UIButton!
    private var contentView: UIView!
    private let attributedText: NSAttributedString

    private lazy var paragraphStyle = {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 3         // 行间距
        paragraphStyle.paragraphSpacing = 8    // 段落之间的间距
        paragraphStyle.alignment = .left        // 对齐方式
        return paragraphStyle
    }()
    

//    let attributedText = NSAttributedString(
//        string: message,
//        attributes: [
//            .font: UIFont.systemFont(ofSize: 16),
//            .foregroundColor: UIColor.white,
//            .paragraphStyle: paragraphStyle
//        ]
//    )
    
    init(title: String, attributedText: NSAttributedString) {
        self.attributedText = attributedText
        super.init(nibName: nil, bundle: nil)
        self.title = title
        modalPresentationStyle = .custom
    }

    private let moveDistance: CGFloat = 50
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        
        maskView = UIButton(frame: view.bounds).addTo(view)
        maskView.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
        maskView.backgroundColor = .black.withAlphaComponent(0)
        
        let contentW = view.width - 120
        
        let maxContentH = screenHeight * 0.5
        var contentH = maxContentH
        
        contentView = UIView().addTo(view)
        contentView.backgroundColor = UIColorMake(0x212529)
        contentView.addCorner(radius: 15)
        
        let scrollLabel = ScrollableLabel()
        scrollLabel.attributedText = attributedText
        
        let closeButton = UIButton(type: .system)
        closeButton.setImage(UIImage(systemName: "xmark"), for: .normal)
        closeButton.tintColor = .white
        closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
        contentView.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(30)
        }
        
        let textLabel = UILabel(font: UIFontMake(16), textColor: .white).addTo(contentView)
        textLabel.text = title
        textLabel.snp.makeConstraints { make in
            make.left.equalTo(AppStyle.margin.left)
            make.right.equalTo(closeButton.snp.left).offset(-10)
            make.centerY.equalTo(closeButton)
        }
        
        let textSize = attributedText.getSize(maxWidth: contentW - AppStyle.margin.left * 2)
        if textSize.height > maxContentH {
             contentH = maxContentH
        } else {
            contentH = textSize.height
        }

        contentView.addSubview(scrollLabel)
        scrollLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(50)
            make.height.equalTo(contentH)
        }
        
        contentH += 80
        contentView.frame = CGRectMake((view.width - contentW) * 0.5, -contentH , contentW, contentH)
        
        contentView.y = (self.view.height - self.contentView.height) * 0.5 - 35 - moveDistance
        contentView.alpha = 0

    }
    
    @objc func confirmButtonTapped() {
        dismiss()
    }
    
    @objc func closeButtonTapped() {
        dismiss()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func showIn(_ controller: UIViewController) {
        controller.present(self, animated: false, completion: nil)
    }

    private func dismiss(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.25) {
            self.contentView.alpha = 0
            self.maskView.backgroundColor = .black.withAlphaComponent(0)
            self.contentView.y = self.contentView.y + self.moveDistance
        } completion: { finishi in
            self.dismiss(animated: false, completion: completion)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        UIView.animate(withDuration: 0.65, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: 0.7, options: .curveEaseIn) {
            self.contentView.alpha = 1
            self.maskView.backgroundColor = .black.withAlphaComponent(0.5)
            self.contentView.y = self.contentView.y + self.moveDistance
        }
    }
    

}
